﻿using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using IotClient.Unity;
using IotClient.View;
using IotClient.VM;
using IotControl.ViewModels;

namespace IotControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitColck();
            VM.LogCall = LogCall;
            Loaded += MainWindow_Loaded;
            Closed += MainWindow_Closed;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (File.Exists("ControlSetting.json"))
                {
                    VM.Setting = ControlSetting.Deserialize(File.ReadAllText("ControlSetting.json"));
                    VM.Setting.ConnentCall = VM.MQTTUnity.ConnentCall;
                }
            }
            catch (Exception ex)
            {
            }
            var win = new SettingWin(
                VM.Setting,
                () =>
                {
                    VM.Connect();
                }
            );
            win.Show();
        }

        private void MainWindow_Closed(object? sender, EventArgs e)
        {
            VM.Setting.Enable = false;
            File.WriteAllText("ControlSetting.json", VM.Setting.Serialize());
        }

        private void LogCall(string msg, bool newLine)
        {
            if (newLine)
                txtAlarm.Text += $"\n[{MQTTUnity.GetTime()}]";
            txtAlarm.Text += msg;
            txtAlarm.ScrollToEnd();
        }

        private DispatcherTimer timer;

        private void InitColck()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(300); // 设置时间间隔为3秒
            timer.Tick += Timer_Tick; // 绑定Tick事件
            timer.Start();
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            txtTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            WindowState =
                this.WindowState == WindowState.Maximized
                    ? WindowState.Normal
                    : WindowState.Maximized;
        }

        private bool _isDragging = false;
        private Point _startPoint;
        private DateTime _lastClickTime = DateTime.Now;
        private const int DoubleClickTime = 500; // 双击时间间隔，单位为毫秒

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DateTime currentClickTime = DateTime.Now;
            TimeSpan timeSinceLastClick = currentClickTime - _lastClickTime;
            if (timeSinceLastClick.TotalMilliseconds <= DoubleClickTime)
            {
                _isDragging = false;
                Border_MouseDown(sender, e);
            }
            else
            {
                _isDragging = true;
                _startPoint = e.GetPosition(this);
                ((UIElement)sender).CaptureMouse();
            }
            _lastClickTime = currentClickTime;
        }

        private void Border_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isDragging)
            {
                Point currentPosition = e.GetPosition(this);
                double offsetX = currentPosition.X - _startPoint.X;
                double offsetY = currentPosition.Y - _startPoint.Y;

                this.Left += offsetX;
                this.Top += offsetY;
            }
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isDragging = false;
            ((UIElement)sender).ReleaseMouseCapture();
        }
    }
}
